package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.GWCDao;
import com.aaa.qy127.entity.Dl;
import com.aaa.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository("gwcDaoImpl")
public class GWCDaoImpl implements GWCDao {
    /**
     * 插入一条 购物信息
     * @param bookid
     * @param count
     * @param outprice
     * @return
     */
    @Override
    public int insertOneGwc(String bookid, String count, String outprice, Integer userid) {
        //System.out.println(outprice);
        int fuhao = outprice.lastIndexOf("￥");
        String substring = outprice.substring(fuhao + 1);
        //System.out.println(substring);
        String sql = "insert into t_gw values(null,?,?,?,?)";
        int i = JDBCUtil.update(sql,bookid,count,substring,userid);
        return i;
    }

    /**
     * 查询 出所有的 购物车 的信息
     * @return
     */
    @Override
    public List<Map<String, Object>> selectAllGWC(Integer userid) {
        String sql = "select tg.gwcid,tg.bookid,tb.bookname,tb.price,tg.count,tb.url,tg.userid from t_gw tg INNER JOIN t_book tb ON tg.bookid = tb.bookid where userid = ? and tb.status";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql,userid);
        if (maps.size() >0){
            return maps;
        }
        return null;
    }

    /**
     * 通过 gwcid 修改 gwc 中的 值
     * @param gwcid
     * @return
     */
    @Override
    public int updatecountByGwcid(String gwcid,String count) {
        String sql = "update t_gw set count = ? where gwcid = ?";
        int i = JDBCUtil.update(sql, count, gwcid);
        return i;
    }

    /**
     * 按照 gwcid 删除 一条 购物车 中的信息
     * @param gwcid
     * @return
     */
    @Override
    public int deleteGwcByGwcid(String gwcid) {
        String sql = "delete from t_gw where gwcid = ?";
        int i = JDBCUtil.update(sql, gwcid);
        return i;
    }

    /**
     * 根据 gwcid 查询 购物车信息
     * @param gwcid
     * @return
     */
    @Override
    public List<Map<String, Object>> getGWCByGwcid(String gwcid) {
        //System.out.println(gwcid);
        List<Map<String, Object>> maps = new ArrayList<>();
        String[] split = gwcid.split(",");
        if (split.length>0){
            for (String s : split) {
                String sql = "select gwcid,bookid,count,outprice,userid from t_gw where gwcid = ?";
                List<Map<String, Object>> maps1 = JDBCUtil.queryMap(sql, s);
                //System.out.println(maps1);
                if (maps1.size() >0){
                    Map<String, Object> map = maps1.get(0);
                    //System.out.println(map);
                    maps.add(map);
                }
            }
        }
        //System.out.println(maps);
        return maps;
    }

    /**
     * 根据 gwcid  删除 购物车 信息
     * @param gwcid
     */
    @Override
    public void deleteGWCByGwcid(String gwcid) {
        String[] split = gwcid.split(",");
        if (split.length>0){
            for (String s : split) {
                String sql = "delete from t_gw where gwcid = ?";
                int i = JDBCUtil.update(sql,s);
            }
        }

    }


}
